package com.my.action;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.annotation.SystemOperatorLog;
import com.my.constant.ErrorCode;
import com.my.entity.Employee;
import com.my.entity.Page;
import com.my.entity.ResponseData;
import com.my.service.EmployeeService;
import com.my.util.CookieUtil;
import com.sun.org.apache.xml.internal.security.utils.Base64;

/**
 * 用于跳转页面
 * 
 * @author wb-zc189961
 *
 */
@Controller
@RequestMapping(value="/emp")
public class EmployeeAction {
	private static final Logger logger = LoggerFactory.getLogger(EmployeeAction.class);
	@Resource
	private EmployeeService employeeService;

	
	@RequestMapping(value="/goto_update_emp")
	public String gotoUpdateEmp(HttpServletRequest request,Long id){
		Employee employee=employeeService.selectByPrimaryKey(id);
		request.setAttribute("employee", employee);
		return "emp/update_emp";
	}
	/**
	 * 查看所有员工
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "emp_list")
	public String selectAllEmp(HttpServletRequest request) {
		List<Employee> empList = employeeService.selectByExample(null);
		request.setAttribute("empList", empList);
		return "emp/emp_list";
	}

	@RequestMapping(value="/goto_add_emp")
	public String gotoAddEmp(){
		return "emp/add_emp";
	}
	
	/**
	 * 查看所有员工（分页）
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "emp_list_for_page")
	public String selectAllEmpForPage(HttpServletRequest request, Page page) {
		ResponseData<Employee> result = employeeService
				.selectAllEmpForPage(page);
		request.setAttribute("result", result);
		return "emp/emp_list";
	}

	/**
	 * 查看员工明细
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("emp_detail")
	public @ResponseBody ResponseData<Employee> queryDetailById(
			HttpServletRequest request, Long id) {
		try {
			Employee employee = employeeService.queryDetailById(id);
			return new ResponseData<Employee>(ErrorCode.SUCCESS,
					ErrorCode.SUCCESS.getDesc(), Arrays.asList(employee),
					false, null);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<Employee>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true, null);
		}
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("delete_emp")
	@SystemOperatorLog(description="删除员工信息")
	public @ResponseBody ResponseData<Integer> deleteEmployee(Long id) {
		try {
			Assert.notNull(id);
			Employee record=employeeService.selectByPrimaryKey(id);
			record.setIsDelete("y");
			employeeService.updateByPrimaryKeySelective(record);
			return new ResponseData<Integer>(ErrorCode.SUCCESS,
					ErrorCode.SUCCESS.getDesc(), null, false,null);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<Integer>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}

	}
	
	/**
	 * 查询单个
	 * 
	 * @return
	 */
	@RequestMapping(value = "select_emp")
	public @ResponseBody ResponseData<Employee> selectLeaveBill(Long id,
			HttpServletRequest request) {
		try {
			Employee employee = employeeService.selectByPrimaryKey(id);
			return new ResponseData<Employee>(ErrorCode.SUCCESS,
					ErrorCode.SUCCESS.getDesc(), Arrays.asList(employee),
					false,null);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<Employee>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}
	}
	
	
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping("update_emp")
	@SystemOperatorLog(description="修改员工信息")
	public @ResponseBody ResponseData<Integer> updateLeaveBill(
			Employee employee, HttpServletRequest request) {
		try {
			if(employee.getId()==0){
				return new ResponseData<Integer>(ErrorCode.FAUILURE,
						ErrorCode.NOT_DATA_FOUND.getDesc(), null, true,null);
			}else{
				employee.setPassword(Base64.encode(CookieUtil.getMD5(employee.getPassword()).getBytes()));
				employeeService.updateByPrimaryKeySelective(employee);
				return new ResponseData<Integer>(ErrorCode.SUCCESS,
						ErrorCode.SUCCESS.getDesc(), null, false,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<Integer>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}
	}
	
	
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping("restore_emp")
	@SystemOperatorLog(description="恢复员工信息")
	public String restoreEmp(Long id) {
		Employee record=employeeService.selectByPrimaryKey(id);
		record.setIsDelete("n");
		employeeService.updateByPrimaryKeySelective(record);
		return "redirect:http://localhost:8888/simple_activiti/common/query_is_delete.do";
	}
	

}